package calvin.study.rabbitmq.advanced.return_queue;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

import calvin.study.rabbitmq.base.RabbitMqBase;

public class Consumer extends RabbitMqBase {

	public static void main(String[] args) throws IOException {
		Channel channel = getChannel();

		DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("消费者接收消息：" + new String(body));
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if ((Integer) properties.getHeaders().get("num") == 0) {
					/**
					 * @param multiple 是否是批量
					 * @param requeue  重新添加到队列尾部
					 */
					channel.basicNack(envelope.getDeliveryTag(), false, true);
				} else {
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
		};

		channel.basicConsume("topic_queue2", false, defaultConsumer);

		// close();
	}

}
